import { Controller, Get, Post, Body, Patch, Param, Delete, UseGuards } from '@nestjs/common';
import { BannerService } from './banner.service';
import { CreateBannerDto } from './dto/create-banner.dto';
import { UpdateBannerDto } from './dto/update-banner.dto';
import { Banner } from './entities/banner.entity';
import { ApiTags, ApiOperation, ApiResponse, ApiParam, ApiBearerAuth } from '@nestjs/swagger';
import { AuthGuard } from 'src/core/auth/auth.guard';

/**
 * 轮播图控制器
 * 提供轮播图的CRUD操作及相关业务逻辑接口
 */
@ApiTags('轮播图')
@Controller('banner')
export class BannerController {
  constructor(private readonly bannerService: BannerService) {}

  /**
   * 创建新的轮播图
   * @param createBannerDto 轮播图数据
   * @returns 创建的轮播图
   */
  @Post()
  @UseGuards(AuthGuard)
  @ApiBearerAuth()
  @ApiOperation({ summary: '创建新的轮播图' })
  @ApiResponse({ status: 201, description: '轮播图创建成功', type: Banner })
  @ApiResponse({ status: 400, description: '请求参数错误' })
  @ApiResponse({ status: 401, description: '未授权，需要登录' })
  async create(@Body() createBannerDto: CreateBannerDto) {
    return this.bannerService.create(createBannerDto);
  }

  /**
   * 获取所有轮播图
   * @returns 轮播图列表
   */
  @Get()
  @UseGuards(AuthGuard)
  @ApiBearerAuth()
  @ApiOperation({ summary: '获取所有轮播图' })
  @ApiResponse({ status: 200, description: '获取成功', type: [Banner] })
  @ApiResponse({ status: 401, description: '未授权，需要登录' })
  async findAll() {
    return this.bannerService.findAll();
  }

  /**
   * 获取激活状态的轮播图
   * @returns 激活状态的轮播图列表
   */
  @Get('active')
  // @UseGuards(AuthGuard)
  @ApiOperation({ summary: '获取激活状态的轮播图' })
  @ApiResponse({ status: 200, description: '获取成功', type: [Banner] })
  async findActive() {
    return this.bannerService.findActive();
  }

  /**
   * 根据ID获取轮播图
   * @param id 轮播图ID
   * @returns 轮播图对象
   */
  @Get(':id')
  @UseGuards(AuthGuard)
  @ApiBearerAuth()
  @ApiOperation({ summary: '根据ID获取轮播图' })
  @ApiParam({ name: 'id', description: '轮播图ID' })
  @ApiResponse({ status: 200, description: '获取成功', type: Banner })
  @ApiResponse({ status: 404, description: '轮播图不存在' })
  @ApiResponse({ status: 401, description: '未授权，需要登录' })
  async findOne(@Param('id') id: string) {
    return this.bannerService.findOne(+id);
  }

  /**
   * 更新轮播图
   * @param id 轮播图ID
   * @param updateBannerDto 更新数据
   * @returns 更新后的轮播图
   */
  @Patch(':id')
  @UseGuards(AuthGuard)
  @ApiBearerAuth()
  @ApiOperation({ summary: '更新轮播图' })
  @ApiParam({ name: 'id', description: '轮播图ID' })
  @ApiResponse({ status: 200, description: '更新成功', type: Banner })
  @ApiResponse({ status: 400, description: '请求参数错误' })
  @ApiResponse({ status: 404, description: '轮播图不存在' })
  @ApiResponse({ status: 401, description: '未授权，需要登录' })
  async update(
    @Param('id') id: string,
    @Body() updateBannerDto: UpdateBannerDto,
  ) {
    return this.bannerService.update(+id, updateBannerDto);
  }

  /**
   * 删除轮播图
   * @param id 轮播图ID
   * @returns 删除结果
   */
  @Delete(':id')
  @UseGuards(AuthGuard)
  @ApiBearerAuth()
  @ApiOperation({ summary: '删除轮播图' })
  @ApiParam({ name: 'id', description: '轮播图ID' })
  @ApiResponse({ status: 200, description: '删除成功' })
  @ApiResponse({ status: 404, description: '轮播图不存在' })
  @ApiResponse({ status: 401, description: '未授权，需要登录' })
  async remove(@Param('id') id: string) {
    return this.bannerService.remove(+id);
  }

  /**
   * 更新轮播图的激活状态
   * @param id 轮播图ID
   * @param isActive 是否激活
   * @returns 更新后的轮播图
   */
  @Patch(':id/status')
  @UseGuards(AuthGuard)
  @ApiBearerAuth()
  @ApiOperation({ summary: '更新轮播图的激活状态' })
  @ApiParam({ name: 'id', description: '轮播图ID' })
  @ApiResponse({ status: 200, description: '状态更新成功', type: Banner })
  @ApiResponse({ status: 404, description: '轮播图不存在' })
  @ApiResponse({ status: 401, description: '未授权，需要登录' })
  async updateStatus(
    @Param('id') id: string,
    @Body('isActive') isActive: boolean,
  ) {
    return this.bannerService.updateStatus(+id, isActive);
  }

  /**
   * 批量更新轮播图排序
   * @param banners 包含id和order的轮播图数组
   */
  @Patch('order')
  @UseGuards(AuthGuard)
  @ApiBearerAuth()
  @ApiOperation({ summary: '批量更新轮播图排序' })
  @ApiResponse({ status: 200, description: '排序更新成功' })
  @ApiResponse({ status: 400, description: '请求参数错误' })
  @ApiResponse({ status: 401, description: '未授权，需要登录' })
  async updateOrder(@Body() banners: { id: number; order: number }[]) {
    return this.bannerService.updateOrder(banners);
  }
}
